/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.boundary;

import giuliani_mapelli_marocco.business.UserManager;
import giuliani_mapelli_marocco.business.EventManager;
import giuliani_mapelli_marocco.business.TimeTools;
import giuliani_mapelli_marocco.entity.Event;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

@Named
@ViewScoped
public class CalendarBean implements Serializable{

    /**
     * Creates a new instance of CalendarBean
     */
    public CalendarBean() {
    }
    
    @EJB
    UserManager um;
    
    @EJB
    EventManager em;
    
    private ScheduleModel eventModel; 
    private ScheduleEvent scheduleEvent = new DefaultScheduleEvent();
    private Event event;
 
    /**
     * Initializes the calendar, adding all the events that the user have created,
     * to which he was invited, in which it participates.
     */
    @PostConstruct
    public void init() {
        eventModel = new DefaultScheduleModel();
        TimeTools t = new TimeTools();
        List<Event> events=  em.getAllUserEvent(um.getLoggedUser().getUsername());
        for(Event e: events) {
            if(!(e.getDeleted())) {
                Date startDate = t.mergeDateAndHour(e.getStartDate(), e.getStartTime());
                Date endDate = t.mergeDateAndHour(e.getEndDate(), e.getEndTime());
                DefaultScheduleEvent defEvent = new DefaultScheduleEvent(e.getName(), startDate, endDate);
                defEvent.setData(e.getId());
                if(e.getOrganizer().equals(um.getLoggedUser()))
                    defEvent.setStyleClass("eventOrganizer");
                else defEvent.setStyleClass("eventParticipant");
                eventModel.addEvent(defEvent);
            }
        }
    }
     
    public ScheduleModel getEventModel() {
        return eventModel;
    }
     
    public ScheduleEvent getScheduleEvent() {
        return scheduleEvent;
    }
 
    public void setScheduleEvent(ScheduleEvent scheduleEvent) {
        this.scheduleEvent = scheduleEvent;
    }
    
    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    /**
     * Handle the "event select" events, saving the selected event in the bean.
     * @param selectEvent event selected
     */
    public void onEventSelect(SelectEvent selectEvent) {
        scheduleEvent = (ScheduleEvent) selectEvent.getObject();
        event = em.findEventById((Long)scheduleEvent.getData());
    }
}
